## Take away from this exercise is that we cannot do anything to identify our 
## estimates against another seasonal pattern that has the same frequency and 
## phase, regardless of amplitude. We can, however, test to see how robust we 
## are to phase shifting and seasonal patterns of alternative frequencies. We 
## can do this by simply creating synthetic control variables with a given 
## trigonometric structure. We should also note that sunset time is offset 
## slightly from the beginning of the year, so we are not quite in phase with 
## the calendar.


## Correlation
## Phase shifting
vals <- seq(0,2*pi,by=.01)
offset <- seq(-2*pi,2*pi, by=2*pi/16)
base <- cos(vals)
j <- 1
corr_off <- rep(0,length(offset))
for(i in offset){
  offset_pattern <- cos(vals+i)
  corr_off[j] <- cor(base, offset_pattern)
  j <- j + 1
}
plot(y=corr,x=offset)

## Amplitude
vals <- seq(0,2*pi,by=.01)
amplitude <- seq(-2*pi,2*pi, by=2*pi/16)
base <- cos(vals)
plot(base)
j <- 1
corr_amp <- rep(0,length(amplitude))
for(i in amplitude){
  amp_pattern <- i*cos(vals)
  corr_amp[j] <- cor(base, amp_pattern)
  j <- j + 1
}
plot(y=corr_amp,x=amplitude)

## Frequency
vals <- seq(0,2*pi,by=.01)
frequency <- seq(0,2*pi, by=2*pi/32)
base <- cos(vals)
j <- 1
corr_freq <- rep(0,length(frequency))
for(i in frequency){
  freq_pattern <- cos(vals*i)
  ##plot(freq_pattern)  
  corr_freq[j] <- cor(base, freq_pattern)
  j <- j + 1
}
plot(y=corr_freq,x=frequency)
plot(base, type='l')
lines(freq_pattern, col='blue')

## Completely spurious pattern
vals <- seq(0,2*pi,by=.05)
offset <- seq(-2*pi,2*pi, by=2*pi/16)
base <- cos(vals)
j <- 1
b_off <- rep(0,length(offset))
for(i in offset){
  offset_pattern <- cos(vals+i)
  y <- offset_pattern + rnorm(length(vals))
  b_off[j] <- lm(y~base)$coefficients[2]
  j <- j + 1
}
plot(y=b_off,x=offset)








